package com.xt.base.wireless.server.udp;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketAddress;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.xt.base.exception.SystemError;
import com.xt.base.reflection.BaseReflector;
import com.xt.base.util.SpringUtil;
import com.xt.base.wireless.server.MobileActionUtil;

/**
 * <br>
 * <br>
 * 
 * <p>
 * <a href=".java.html"><i>查看源文件</i></a>
 * </p>
 * 
 * @author 杨洪波
 * @create-date:2010-9-12
 */
public class UDPServerThread extends Thread {
	Logger log = Logger.getLogger(getClass());
	
	private long clientNum = 0;
	
	private SocketAddress address;
	
	private String receiveMsg;

	public UDPServerThread(SocketAddress address,String receiveMsg, long num) { // 构造函数
		this.address = address; // 初始化SocketAddress变量
		clientNum = num; // 初始化clientnum变量
		this.receiveMsg = receiveMsg;
		this.start();
		log.info("第"+clientNum+"个活动UDP链接已建立。SocketAddress="+address);
	}
	
	public void run(){
		execute(receiveMsg);
	}
	
	private DatagramPacket sendDgp = null;
	byte[] sendBytes = null;
	DatagramSocket sendDs = null;
	String sendMsg = null;
	private void send(Map<String,Object> map){
		sendMsg = MobileActionUtil.parseResultMap2Xml(map);
		try {
			sendDs = new DatagramSocket();
			sendBytes = sendMsg.getBytes("UTF-8");
			sendDgp = new DatagramPacket(sendBytes, sendBytes.length,address);
			sendDs.send(sendDgp);
			
		} catch (IOException ioe) {
			ioe.printStackTrace();
		}finally{
			sendMsg = null;
			sendBytes = null;
			sendDgp = null;
			if(sendDs!=null) {
				sendDs.close();
				sendDs = null;
			}
		}
	}
	
	@SuppressWarnings("unchecked")
	public void execute(String xml){
		
		log.info("-----------传入 UDPServerThread.execte的参数xml= "+xml);
		
		Map<String,Object> map = MobileActionUtil.parseRequestXml(xml);
		if(map.get("action")==null){
			throw new SystemError(new Object(),new Object[]{xml},"没有指定Action！");
		}
		if(map.get("method")==null){
			throw new SystemError(new Object(),new Object[]{xml},"没有指定Method！");
		}
		
		Object o = null;
		try{
			o = SpringUtil.getWebBean(String.valueOf(map.get("action")));
		}catch(Exception e){
			e.printStackTrace();
			throw new SystemError(this,new Object[]{},"指定的Action不正确，或者没有为Spring配置Action！");
		}
		try{
			log.info("开始调用业务代码，获取数据");
			o = BaseReflector.invoke(o, String.valueOf(map.get("method")), map, Map.class);
			log.info("调用业务代码结束，已取得数据，得到一个对像o="+o);
		}catch(Exception e){
			e.printStackTrace();
			throw new SystemError(this,new Object[]{},"指定的Method不正确，或者Method的参数类型不匹配！");
		}
		if(o!=null && o instanceof Map<?,?>){//有具体返回值时，才需要解析为xml文件，并处理回调。
			this.send((Map<String,Object>)o);
			log.info("MOA服务器端已响应手机端的请求，返回结果集");
		}else{
			map = new HashMap<String,Object>();
			map.put("baded", "yes");
			this.send(map);
			log.info("MOA服务器端已响应手机端的请求，返回服务器端异常标识。");
		}
	}
}
